package edu.joe.demo.util;

import util.common.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {
    /**
     * 读取文件
     * @param path
     * @return
     */
    public static List<String> readAllLines(String path){
        try{
            return readAllLines(new FileInputStream(new File(path)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<String> readAllLines(InputStream stream){
        BufferedReader in = null;
        try {
            List<String> lines = new ArrayList<>();
            BufferedInputStream bis = new BufferedInputStream(stream);
            in = new BufferedReader(
                    new InputStreamReader(bis, "utf-8"), 1024 * 1024);//1M缓存
            while (in.ready()) {
                lines.add(in.readLine());
            }
            return lines;
        } catch (IOException ex) {
            ex.printStackTrace();
        }finally {
            if (in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static String readAllText(String path){
        List<String> lines = readAllLines(path);
        return StringUtils.join("\n", lines);
    }

    public static String readAllText(InputStream stream){
        List<String> lines = readAllLines(stream);
        return StringUtils.join("\n", lines);
    }

    public static void writeAllText(String path, String content){
        BufferedWriter bw = null;
        try {
            bw = connectAndGetWriter(path);
            bw.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void writeAllLines(String path, List<String> lines){
        BufferedWriter bw = null;
        try {
            bw = connectAndGetWriter(path);
            for (String str : lines){
                bw.write(str);
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static File getFile(String path){
        return new File(path);
    }

    /**
     * 连接文件，并获取缓存写入通道
     * @param path
     * @return
     * @throws IOException
     */
    private static BufferedWriter connectAndGetWriter(String path) throws IOException {
        File file = new File(path);
        if(!file.exists()){
            file.createNewFile();
        }
        FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
        return new BufferedWriter(fileWriter);
    }
}
